package cn.com.controller;

import cn.com.Authorization;
import cn.com.beans.PResource;
import cn.com.service.PResourceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/resource")
public class ResourceContorller implements BaseController {
    @Resource
    private PResourceService resourceService;

    @GetMapping
    public String home() {
        return getModelName()+"/index";
    }

    @Override
    public String getModelName() {
        return "resource";
    }


    @GetMapping(headers = "X-Requested-With=XMLHttpRequest")
    @ResponseBody
    @Authorization(url = "/resource",authority = "r",methods = RequestMethod.GET)
    public Map<String,Object> findAll(PResource resource){
        List<PResource> resources = resourceService.findAll(resource);
        Map<String,Object> result = new HashMap<>();
        result.put("data",resources);
        result.put("code",0);
        result.put("count",resources.size());
        result.put("msg","查询成功");
        return result;
    }
    @GetMapping({"/edit","/edit/{id}"})
    @Authorization(url = "/resource",authority = "r",methods = RequestMethod.GET)
    public String editor(@PathVariable(required = false) Integer id,@RequestParam(defaultValue = "-1") Integer pid, Model model){
        PResource resource = null;
        if(id != null){
            resource = this.resourceService.findByid(id);

        }else {
            resource = new PResource();
            resource.setParent(this.resourceService.findByid(pid));
            System.out.println(resource);
        }
        model.addAttribute("resource",resource);
        return getModelName()+"/editor";
    }
    @PutMapping
    @ResponseBody
    @Authorization(url = "/resource",authority = "c",methods = RequestMethod.PUT)
    public Map<String,Object> save(PResource resource){
        Map<String,Object> results = new HashMap<>();
        if(resource.getId()!= null){
            if(resourceService.update(resource)){
                results.put("code",0);
                results.put("msg","修改成功");
            }else{
                results.put("code",1);
                results.put("msg","修改失败");
            }
        }else{
            resource.setPid(-1);
            if(resourceService.insert(resource)){
                results.put("code",0);
                results.put("msg","新增成功");
            }else{
                results.put("code",1);
                results.put("msg","新增失败");
            }
        }
        return results;
    }
    @DeleteMapping
    @ResponseBody
    @Authorization(url = "/resource",authority ="d",methods = RequestMethod.DELETE)
    public Map<String,Object> delete(@RequestParam("id") Integer[] ids){
        Map<String,Object> resoult = new HashMap<>();
        if(resourceService.deleteByIds(ids)){
            resoult.put("code",0);
            resoult.put("msg","删除成功");
        }else {
            resoult.put("code",1);
            resoult.put("msg","删除失败");
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return resoult;
    }
}
